package com.jg.xmlSchema2JsonSchema.rules;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jg.xmlSchema2JsonSchema.xsd.Constants;
import com.jg.xmlSchema2JsonSchema.xsd.XsComplexType;
import com.jg.xmlSchema2JsonSchema.xsd.XsElement;

/**
 * @author czg
 */
public class ObjectRule extends Rule {
    private XsElement element;
    private XsComplexType complexType;

    public ObjectRule() {
        // TODO Auto-generated constructor stub
        super(null);
    }

    public ObjectRule(ObjectRule parentObjectRule, XsElement element) {
        // TODO Auto-generated constructor stub
        super(parentObjectRule);
        this.element = element;
    }

    public ObjectRule(ObjectRule parentObjectRule, XsComplexType complexType) {
        // TODO Auto-generated constructor stub
        super(parentObjectRule);
        this.complexType = complexType;
    }

    @Override
    public JSONObject apply() {
        if (ObjectUtil.isNotNull(element)) {
            System.out.println(StrUtil.format("element:{}", element.getName()));
            String description = element.getAnnotation();
            if (StrUtil.isNotEmpty(description)) {
                jsonObject.put(Constants.K_W_description, description);
            }
            if (StrUtil.isNotEmpty(element.getType())) {
                jsonObject.put(Constants.K_W_$ref, StrUtil.format("#/{}/{}", Constants.K_W_definitions, element.getType()));
            } else {
                putType(Constants.O_T_object);
            }
        }
        if (ObjectUtil.isNotNull(complexType)) {
            putType(Constants.O_T_object);
            System.out.println(StrUtil.format("public complexType:{}", complexType.getName()));
            String description = complexType.getAnnotation();
            if (StrUtil.isNotEmpty(description)) {
                jsonObject.put(Constants.K_W_description, description);
            }
        }
        return jsonObject;
    }

    public void addRequired(String name) {
        if (!jsonObject.containsKey(Constants.K_W_required)) {
            jsonObject.put(Constants.K_W_required, new JSONArray());
        }
        jsonObject.getJSONArray(Constants.K_W_required).add(name);
    }

    public void putProperty(String name, RuleInterface rule) {
        // TODO Auto-generated method stub
        if (!jsonObject.containsKey(Constants.K_W_properties)) {
            jsonObject.put(Constants.K_W_properties, new JSONObject());
        }
        jsonObject.getJSONObject(Constants.K_W_properties).put(name, rule.getJsonObject());
    }

    public void addDefinition(String name, ObjectRule objectRule) {
        addDefinition(name, objectRule.getJsonObject());
    }

    public void addDefinition(String name, EnumRule enumRule) {
        addDefinition(name, enumRule.getJsonObject());
    }

    private void addDefinition(String name, JSONObject jsonObject) {
        if (!getJsonObject().containsKey(Constants.K_W_definitions)) {
            getJsonObject().put(Constants.K_W_definitions, new JSONObject());
        }
        getJsonObject().getJSONObject(Constants.K_W_definitions).put(name, jsonObject);
        System.out.println(StrUtil.format("addDefinition:{}", name));
    }
    public boolean existDefinition(String name) {
        if (!getJsonObject().containsKey(Constants.K_W_definitions)) {
            return false;
        }
        return getJsonObject().getJSONObject(Constants.K_W_definitions).containsKey( name);
    }

    public void put$schema() {
        jsonObject.put(Constants.K_W_$schema, "http://json.schema.org/draft.04/schema#");
    }

}
